//
//  TTValidator.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/9.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import Foundation

class TTValidator {
    
    class func validateMobile(_ mobile: String) -> Bool {
        guard mobile.hasPrefix("10") == false else {
            return false
        }
        guard mobile.hasPrefix("11") == false else {
            return false
        }
        guard mobile.hasPrefix("12") == false else {
            return false
        }
        let phoneReg = "^1\\d{10}"
        let regexTextMobile = NSPredicate(format: "SELF MATCHES %@", phoneReg)
        return regexTextMobile.evaluate(with: mobile)
    }
    
    //  MARK: *****判断是否为金额
    
    class func validatorPrice(_ text:String) -> Bool {
        let expression = "^[0-9]+$|^[0-9]+\\.[0-9]{1,2}$"
        let regex = try! NSRegularExpression(pattern: expression, options: NSRegularExpression.Options.allowCommentsAndWhitespace)
        let numberOfMatches = regex.numberOfMatches(in: text, options:NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, (text as NSString).length))
        return numberOfMatches != 0
    }
    
    class func validatorPrice0to7(_ text:String) -> Bool {
        let pri:Double = Double(text)!
        if pri > 0.00 && pri <= 7.00 {
            return true
        } else {
            return false
        }
    }
    
    //  MARK: *****非负浮点数
    class func validateFloat(_ value: String) -> Bool {
        
        return validate(value:value,preStr: "^\\d+(\\.\\d+)?$")
        
    }
    
    //  MARK: *****不少于6位的数字密码
    class func validatePassword(_ value: String) -> Bool {
        
        return !validate(value:value,preStr: "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$")
        
    }
    
    //  MARK: *****判断6位数字密码
    class func isSixDigital(_ value: String) -> Bool {
        
        return validate(value:value,preStr: "^\\d{6}$")
        
    }
    
    //  MARK: ***** 判断digit位数字
    class func isDigital(_ value: String,_ digit: NSInteger) -> Bool {
        return validate(value:value,preStr: "^\\d{\(digit)}$")
    }
    
    
    class func validate(value: String,preStr:String) -> Bool{
        
        let predicate = NSPredicate(format: "SELF MATCHES %@" , preStr)
        
        return predicate.evaluate(with: value)
        
    }
    //  MARK: *****邮箱
    class func isEmail(_ string:String) -> Bool {
        
        let pattern = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
        
        return validate(value:string,preStr:pattern)
    }
    //  MARK: *****邮编
    class func isZipCode(_ string:String) -> Bool {
        
        if string.characters.count != 6 {
            return false
        }
        return true
    }
    
    //  MARK: *****手机号码
    class func isTelNumber(num:String)->Bool{
        
        let mobile = "^1[3-9]{1}[0-9]{9}$"
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
        
        if (regextestmobile.evaluate(with: num) == true)
        {
            return true
        }
        else
        {
            return false
            
        }
    }
    
    //  MARK: *****身份证号
    class func isIDCard(num:String)->Bool{
        
        let idcard = "^(\\d{14}|\\d{17})(\\d|[xX])$"
        let regextest = NSPredicate(format: "SELF MATCHES %@",idcard)
        
        return regextest.evaluate(with:num)
    }
    
    //MARK:是否有特殊字符
    class func isDigitalCharater(num:String)->Bool{
        
        let idcard = "[0-9a-zA-Z]"
        let regextest = NSPredicate(format: "SELF MATCHES %@",idcard)
        return regextest.evaluate(with:num)
    }
    
    //MARK:只能为中文
    class func onlyInputChineseCharacters(_ string: String) -> Bool {
        let inputString = "[\u{4e00}-\u{9fa5}]+"
        let predicate = NSPredicate(format: "SELF MATCHES %@", inputString)
        let Chinese = predicate.evaluate(with: string)
        return Chinese
    }
    
    
    
    //MARK:只能为数字
    class func onlyInputTheNumber(_ string: String) -> Bool {
        let numString = "[0-9]*"
        let predicate = NSPredicate(format: "SELF MATCHES %@", numString)
        let number = predicate.evaluate(with: string)
        return number
    }
    
    
    //MARK:只能为小写
    
    class func onlyInputLowercaseLetter(_ string: String) -> Bool {
        let regex = "[a-z]*"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        let letter = predicate.evaluate(with: string)
        return letter
    }
    
    
    //MARK:只能为大写
    
    class func onlyInputACapital(_ string: String) -> Bool {
        let regex = "[A-Z]*"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        let capital = predicate.evaluate(with: string)
        return capital
    }
    
    
    
    //MARK:允许大小写
    class func inputCapitalAndLowercaseLetter(_ string: String) -> Bool {
        let regex = "[a-zA-Z]*"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        let inputString = predicate.evaluate(with: string)
        return inputString
    }
    
    
    //MARK:允许大小写或数字(不限字数)
    
    class func inputLettersOrNumbers(_ string: String) -> Bool {
        let regex = "[a-zA-Z0-9]*"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        let inputString = predicate.evaluate(with: string)
        return inputString
    }
    
    
    
    //MARK:允许大小写或数字(限字数)
    class func inputNumberOrLetters(_ name: String) -> Bool {
        let userNameRegex = "^[A-Za-z0-9]{6,20}+$"
        let userNamePredicate = NSPredicate(format: "SELF MATCHES %@", userNameRegex)
        let inputString = userNamePredicate.evaluate(with: name)
        return inputString
    }
    
    
    
    //MARK:允许汉字，大小写或数字(限字数)
    class func inputChineseOrLettersNumberslimit(_ string: String) -> Bool {
        let regex = "[\u{4e00}-\u{9fa5}]+[A-Za-z0-9]{6,20}+$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        let inputString = predicate.evaluate(with: string)
        return inputString
    }
    
    
    //MARK:允许汉字或数字(不限字数)
    class func inputChineseOrNumbers(_ string: String) -> Bool {
        let regex = "[\u{4e00}-\u{9fa5}]+[0-9]*"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        let inputString = predicate.evaluate(with: string)
        return inputString
    }
    
    //MARK:允许汉字或数字(限字数)
    class func inputChineseOrNumbersLimit(_ string: String) -> Bool {
        let regex = "[\u{4e00}-\u{9fa5}][0-9]{6,20}+$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        let inputString = predicate.evaluate(with: string)
        return inputString
    }

    
    //MARK:允许汉字，大小写或数字(不限字数)
    class func inputChineseOrLettersAndNumbersNum(_ string: String) -> Bool {
        let regex = "[\u{4e00}-\u{9fa5}]+[A-Za-z0-9]*"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        let inputString = predicate.evaluate(with: string)
        return inputString
    }

}
